<?php

/**根据地址，获取经纬度
 * @param  $adderss
 * @return  $mixed
 */
function adderss_lng_lat($address){
    $url = "http://restapi.amap.com/v3/geocode/geo?address='.$address.'&key=<用户的key>";
    if($result = file_get_contents($url)){
        $result = json_decode($result,true);
        //判断是否成功
        if(!empty($result['count'])){
            return  explode(',',$result['geocodes']['0']['location']);

        }else{
            return false;
        }
    }
}

/**根据经纬度，转地址
 * @param  $lng_lat
 * @return  $address
 */

function lng_lat_address($lng_lat){
    $url = "http://restapi.amap.com/v3/geocode/regeo?output=xml&location='.$lng_lat.'&key=<用户的key>&radius=1000&extensions=all";
    if($result = file_get_contents($url)){
        $result = json_decode($result,true);
        if(!empty($result['status'])&&$result['status']==1){

            return $result['regeocode']['formatted_address'];

        }else{
            return false;
        }
    }
}

/**
 * 计算两组经纬度坐标 之间的距离
 * params ：lat1 纬度1； lng1 经度1； lat2 纬度2； lng2 经度2； len_type （1:m or 2:km);
 * return m or km
 */
function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
{
    $radLat1 = $lat1 * 3.1415926 / 180.0;
    $radLat2 = $lat2 * 3.1415926 / 180.0;
    $a = $radLat1 - $radLat2;
    $b = ($lng1 * 3.1415926 / 180.0) - ($lng2 * 3.1415926 / 180.0);
    $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
    $s = $s * 6378.137;
    $s = round($s * 1000);
    if ($len_type > 1)
    {
        $s /= 1000;
    }
    return round($s, $decimal);
}

/*内存单位转换*/
function sizecount($filesize) {
    if($filesize >= 1073741824) {
        $filesize = round($filesize / 1073741824 * 100) / 100 . ' gb';
    } elseif($filesize >= 1048576) {
        $filesize = round($filesize / 1048576 * 100) / 100 . ' mb';
    } elseif($filesize >= 1024) {
        $filesize = round($filesize / 1024 * 100) / 100 . ' kb';
    } else {
        $filesize = $filesize . ' bytes';
    }
    return $filesize;
}

/**对象转数组
* @param $obj
*/
function object_array($array) {
    if(is_object($array)) {
        $array = (array)$array;
    } if(is_array($array)) {
        foreach($array as $key=>$value) {
            $array[$key] = object_array($value);
        }
    }
    return $array;
}


//客户端请求的header
function get_all_headers(){
    // 忽略获取的header数据
    $ignore = array('host','accept','content-length','content-type');
    $headers = array();
    foreach($_SERVER as $key=>$value){
        if(substr($key, 0, 5)==='HTTP_'){
            $key = substr($key, 5);
            $key = str_replace('_', ' ', $key);
            $key = str_replace(' ', '-', $key);
            $key = strtolower($key);

            if(!in_array($key, $ignore)){
                $headers[$key] = $value;
            }
        }
    }

    return $this->curl_post_https(['x-auth-token'=>$headers['x-auth-token']]);
}
/**post请求  单个用户ID获取信息
 * @param  $data
 * @return  $tmpInfo
 */

function post_curl($token){
    $client = new \GuzzleHttp\Client();
    $res = $client->request('POST',"http://192.168.0.63/uc/userInfo/findMe",['headers'=>['x-auth-token'=>$token]]);
    if($res->getStatusCode() != 200){
        return false;
    }else{
        $res = json_decode($res->getBody(),true);
        return $res;
    }

}

/**post请求  多个用户组获取信息
 * @param  $data
 * @return  $tmpInfo
 */

function post_curl_memberids($token,$ids){
    $client = new \GuzzleHttp\Client();
    $res = $client->request('POST',"http://192.168.0.63/uc/userInfo//findMenberByIds",['headers'=>['x-auth-token'=>$token],'form_params'=>['ids'=>$ids]]);
    if($res->getStatusCode() != 200){
        return false;
    }else{
        $res = json_decode($res->getBody(),true);
        return $res;
    }

}
